<template>
  <div class="main">
    <div class="questions">
      <div class="q" :class="type==3?' long':''" v-for="(q,index) in questions" :key="index">
        <span>{{q}}</span> ？
      </div>
    </div>
    <div class="btn">
      <i-button
        i-class="change"
        inline
        @click="getQuestion"
        type="ghost"
        shape="circle"
        size="small"
      >换一批</i-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      titleArr: ['', '加减法练习', '乘法练习', '加减乘除混合运算练习'],
      title: '',
      type: 1,
      range: 10,
      questions: [],
    }
  },
  methods: {
    getQuestion() {
      wx.cloud
        .callFunction({
          name: 'get_question',
          data: {
            type: this.type,
            range: this.range || 10,
          },
        })
        .then(res => {
          this.questions = res.result || []
        })
    },
  },
  onLoad(options) {
    this.type = this.$root.$mp.query.type || decodeURIComponent(options.type)
    this.range = this.$root.$mp.query.range || decodeURIComponent(options.range)
    this.getQuestion()
    this.title = this.titleArr[this.type]
    mpvue.setNavigationBarTitle({ title: this.title })
    wx.cloud.callFunction({
      name: 'do_practice',
      data: {},
    })
  },
  onShow() {},
  onReachBottom() {},
  onShareAppMessage() {
    return {
      title: this.title,
      path: '/pages/practice/main?type=' + this.type + '&range=' + this.range,
    }
  },
  onShareTimeline() {
    return {
      title: this.title,
      path: '/pages/practice/main?type=' + this.type + '&range=' + this.range,
    }
  },
  mounted() {
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline'] })
  },
  onUnload() {
    Object.assign(this.$data, this.$options.data())
  },
}
</script>

<style lang="less" scoped>
page {
  background: #fff;
}
.main {
  padding: 20px;
  .questions {
    display: flex;
    flex-wrap: wrap;
    .q {
      width: 50%;
      text-align: center;
      font-size: 28px;
      margin-bottom: 10px;
      &.long {
        width: 100%;
      }
    }
  }
  .btn {
    text-align: right;
    /deep/ .change {
      padding: 0 25px;
    }
  }
}
</style>
